﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Frogism.Core;
namespace Frogism.Tests
{
    [TestFixture]
    public class BookTest
    {
        [Test]
        public void BookIndexLengthNotNull()
        {
            Book book = new Book();
            Assert.IsNotNull(book.IndexLength);
        }
        [Test]
        public void LastReadDateNoReads() {
          Book book = new Book();
          Assert.AreEqual(DateTime.MinValue, book.LastReadDate);
        }
        [Test]
        public void LastReadDate() {
          Book book = new Book();
          book.Reads.Add(new Read() { Id = 0, ReadDate = new DateTime(2008, 10, 1), Page = 5 });
          book.Reads.Add(new Read() { Id = 0, ReadDate = new DateTime(2008, 9, 1), Page = 10 });

          Assert.AreEqual(new DateTime(2008, 10, 1), book.LastReadDate);
        }
      
        [Test]
        public void DefaultFactor()
        {
            Book book = new Book();
            Assert.AreEqual(1.0m, book.Factor);
        }
        [Test]
        public void BookActualPageBasedOnFactor()
        {
            Book book = new Book();

            book.Reads.Add(new Read() { Id = 0, Page = 1, ReadDate = DateTime.Now });

            book.Factor = 3.3;

            Assert.AreEqual(3.3, book.ActualPage);
        }
        [Test]
        public void BookActualPageBasedOnDefaultFactor()
        {
            Book book = new Book();

            book.Reads.Add(new Read() { Id = 0, Page = 4, ReadDate = DateTime.Now });

           

            Assert.AreEqual(4, book.ActualPage);
        }
        [Test]
        public void BookActualPageBasedOnDefaultFactorWithIndex() {
          Book book = new Book();
          book.IndexLength = 33;
          book.Reads.Add(new Read() { Id = 0, Page = 4, ReadDate = DateTime.Now });



          Assert.AreEqual(4, book.ActualPage);
        }
        [Test]
        public void TotalReadwithIndex()
        {
            Book book = new Book();
            book.IndexLength = 13;
            book.Reads.Add(new Read() { Id = 0, Page = 11, ReadDate = DateTime.Now });
            Assert.AreEqual(13 + 11, book.TotalRead);
        }
        [Test]
        public void TotalReadwithIndexNoReads()
        {
            Book book = new Book();
            book.IndexLength = 13;
           
            Assert.AreEqual(0, book.TotalRead);
        }
        [Test]
        public void LastYearReadNoReads()
        {
            Book book = new Book();
            book.Reads.Add(new Read() { Id = 0, Page = 11, ReadDate = DateTime.Now });
            Assert.AreEqual(0, book.LastYearRead);

        }
        [Test]
        public void LastYearReadWithIndex()
        {
            Book book = new Book();
            book.IndexLength = 13;
            book.Reads.Add(new Read() { Id = 0, Page = 11, ReadDate = DateTime.Now.AddYears(-1).CleanDate() });
            Assert.AreEqual(13+11, book.LastYearRead);

        }
        [Test]
        public void TotalPagesIncludeIndex() {
          Book book = new Book();
          book.IndexLength = 11;
          book.LastPageNumber = 100;
          book.Reads.Add(new Read() { Id = 1, Page = 51, ReadDate = DateTime.Now });

          Assert.AreEqual(111, book.TotalPages);


        }

        [Test]
        public void PercentageDoesIncludeIndex() {
          Book book = new Book();
          book.LastPageNumber = 100;
          book.IndexLength = 13;
          book.Reads.Add(new Read() { Id = 1, Page = 50, ReadDate = DateTime.Now });

          Assert.AreEqual(63m/113*100, book.PercentRead);
        }
        [Test]
        public void BsingrShouldBeFinished() {
          Data.BookLoader bl = new Data.BookLoader();
          Book book = 
            (Book)bl.GetAll().FirstOrDefault(b => b.Title.Contains("Brisingr"));
          Assert.IsNotNull(book, "Can't find Brsinger");
          Assert.AreEqual(100m, book.PercentRead);
        }
        [Test]
        public void PercentageWorksNoReadsAndIndex() {
          Book book = new Book();
          book.LastPageNumber = 100;
          book.IndexLength = 13;
         Assert.AreEqual(0, book.PercentRead);
        }
        
        [Test]
        public void LastYearRead()
        {
            Book book = new Book();
            book.Reads.Add(new Read() { Id = 0, Page = 11, ReadDate = DateTime.Now.AddYears(-1) });
            Assert.AreEqual(11, book.LastYearRead);

        }
        [Test]
        public void RemainingWithIndex() { // error found with TTD 2008.09.29
          Book book = new Book();
          book.IndexLength = 8;
          book.LastPageNumber = 216;
          book.Reads.Add(new Read() { Id = 1, Page = 171, ReadDate = DateTime.Now });

          Assert.AreEqual(216 + 8 - 8 - 171, book.ToGo);
        }
        [Test]
        public void IsFinishedIfToGoIsZero() {
          Book book = new Book();
          book.IndexLength = 10;
          book.IsFinished = false;
          book.LastPageNumber = 100;
          book.Reads.Add(new Read(){Id=1,Page=100,ReadDate=DateTime.Now});
          Assert.AreEqual(0,book.ToGo,"ToGo should be zero");
          Assert.IsTrue(book.IsFinished);

        }
    }
}
